/*
 * McrtCSVReader.cpp
 *
 *  Created on: 29 May 2012
 *      Author: rveitch
 */

#include "CSVReader.h"
#include <stdexcept>

CSVReader::CSVReader(std::string filename)
{
	csv = new std::ifstream(filename.c_str(), std::ifstream::in);
	if(!csv->is_open()){
		std::cout << "File " << filename << "has failed to open";
		throw;
	}
}

CSVReader::~CSVReader()
{
    if(csv->is_open()){
        csv->close();
    }
    delete csv;
}


std::vector<std::string> CSVReader::readLine(){
    std::string line;
    std::vector<std::string> vec;

    if(std::getline(*csv,line)){

        std::stringstream  lineStream(line);
        std::string        cell;

        while(std::getline(lineStream,cell,',')){
            vec.push_back(cell);
        }
    }

    return vec;
}


std::vector<int> CSVReader::readLineInts(){
    std::vector<int> iVec;

    std::vector<std::string> sVec = readLine();

    if(sVec.size() == 0){
    	std::cout <<  "readLine returned empty vector\n";
    	throw;
    }

    for (int iii=0 ; iii<sVec.size() ; iii++){
        iVec.push_back(str2int(sVec[iii]));
    }

    return iVec;
}

int CSVReader::str2int(std::string s){
    return atoi(s.c_str());
}

bool CSVReader::eof(){
    return csv->eof();
}
